package com.leyou.cart.controller;

import com.leyou.cart.pojo.Cart;
import com.leyou.cart.service.CartService;
import com.leyou.item.pojo.Sku;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     *
     * @param cart
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart) {
        Boolean boo = this.cartService.addCart(cart);
        if (boo) {
            return ResponseEntity.status(HttpStatus.CREATED).build();
        }
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
    }

    /**
     * 查询购物车
     */

    @GetMapping
    public ResponseEntity<List<Cart>> queryCartList() {

        List<Cart> carts = this.cartService.queryCart();
        if (carts == null || carts.size() == 0) {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(carts);
    }

    /**
     * 删除商品在redis
     *
     * @param skuId
     * @return
     */
    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId") Long skuId) {
        this.cartService.deleteCart(skuId);
        return ResponseEntity.ok().build();

    }

    /**
     * 增加购物车商品数量
     */

    @PutMapping("increment")
    public ResponseEntity<Void> updateCart(@RequestBody Cart cart) {
        this.cartService.updateIncrementCart(cart);
        return ResponseEntity.ok().build();
    }
}
